from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    _driver = None
    _base_url = ""

    def __init__(self, driver: WebDriver = None):
        #没传 driver 进来，我们自己 new 一个
        if driver is None:
            # 指定 driver 的路径
            service = Service(
                executable_path='/home/lkm/Environment/QA/selenium_driver/chromedriver-linux64/chromedriver')
            self._driver = webdriver.Chrome(service=service)


        # 传了 driver 进来
        else:
            self._driver = driver

        # 跳转到 Base 页a
        if self._base_url != "":
            self._driver.get(self._base_url)

        self._driver.implicitly_wait(3)

    def find(self, by, locator):
        return self._driver.find_element(by, locator)

    def finds(self, by, locator):
        return self._driver.find_elements(by, locator)

    # 显示等待 元素可点击
    def wait_for_click(self,locator,timeout=10):
        WebDriverWait(self._driver,timeout).until(expected_conditions.element_to_be_clickable(locator))
